package com.cheng.controller;

import com.cheng.common.exception.CommonException;
import com.cheng.common.results.DataRslt;
import com.cheng.common.utils.CodeUtil;
import com.cheng.common.utils.IpUtil;
import com.cheng.common.utils.StringUtil;
import com.cheng.data.entity.UserInfoEntity;
import com.cheng.data.vo.LoginLogInfoVO;
import com.cheng.data.vo.UserInfoVO;
import com.cheng.operation.LoginLogInfoOperation;
import com.cheng.operation.LoginOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @author: ChengJW
 * @date: 2021/11/04 18:27
 * @version: 1.0
 * @description: 登录处理器
 */
@Controller
public class LoginController extends BaseController{

    private final static Logger log = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private LoginOperation loginOperation;

    @Autowired
    private LoginLogInfoOperation loginLogInfoOperation;

    /**
    * @author: ChengJW
    * @params: []
    * @return: java.lang.String
    * @date: 2021/11/04
    * @description: 进入登录页面
    */
    @RequestMapping("/login")
    public String login(HttpServletRequest request){
        return "login";
    }

    /**
    * @author: ChengJW
    * @params: [city, request]
    * @return: java.lang.String
    * @date: 2021/12/26
    * @description: 获取游客访问的信息
    */
    @RequestMapping("/visitor")
    @ResponseBody
    public DataRslt visitor(String city,HttpServletRequest request){
        String ip = IpUtil.getIp(request);
        LoginLogInfoVO loginLogInfoVO = new LoginLogInfoVO();
        loginLogInfoVO.setLlNm(getUrNm());
        loginLogInfoVO.setLlCity(city);
        loginLogInfoVO.setLlIp(ip);
        loginLogInfoOperation.insertLoginLogInfo(loginLogInfoVO);
        return success();
    }

    /**
     * @author: ChengJW
     * @params: []
     * @return: com.cheng.common.utils.ResultData
     * @date: 2021/8/22
     * @description: 生成简单的自定义验证码
     */
    @ResponseBody
    @RequestMapping("/showCheckCOde")
    public DataRslt showCheckCode(){

        StringBuilder builder = new StringBuilder("");
        //创建字符数组
        char[]  charArray = CodeUtil.CHECK_STRING.toCharArray();
        int length = charArray.length;
        Random random = new Random();
        for (int i=0;i<4;i++){
            //通过数组长度，限制随机出来的字符索引，并通过索引获取相应的字符
            int index = random.nextInt(length);
            builder.append(charArray[index]);
        }
        return success(builder.toString());
    }

    /**
     * @author: ChengJW
     * @params: [userName, passWord, response]
     * @return: com.cheng.common.utils.ResultData
     * @date: 2021/8/22
     * @description: 账号密码登录
     */
    @ResponseBody
    @RequestMapping("/UserLogin")
    public DataRslt userLogin(String userName, String passWord) throws CommonException{

        if (StringUtil.isNull(userName) || StringUtil.isNull(passWord)){
            log.error("username or password is null");
            return failure("username or password is null");
        }
        UserInfoEntity entity = new UserInfoEntity();
        entity.setUrNm(userName);
        entity.setUrPwd(passWord);
        //登录
        UserInfoVO userInfoVO = new UserInfoVO();
        userInfoVO = loginOperation.userLogin(entity);
        if (null != userInfoVO){
            return success(null);
        }
        return failure(CodeUtil.CODE_MAP.get(CodeUtil.CJW23));
    }
}
